﻿using System;
using System.Collections.Generic;

using UnityEngine;


public class NetMgr
{



    string url1 = "http://123.57.251.66/djembe/organization/getOrganizationByProvinceAndCityAndDistrict.action";
    public static void ReqOrganization()
    {
        WWWForm form = new WWWForm();
        form.AddField("phone", "18244251519");
        NetProxy.GetInst().ReqText(url2, form, (suc, text) => {

            Debug.Log(suc);
            Debug.Log(text);
        });
    }


    public static string url2 = "http://123.57.251.66/djembe/main/sendAuthCode.action";
    public static void ReqPhotoMeg(string number, Action<bool,string> cb)
    {
        WWWForm form = new WWWForm();
        form.AddField("phone", number);
        form.AddField("unique_identifier", DeviceID.Get());
        NetProxy.GetInst().ReqText(url2, form, (suc, text)=> {

            if ( suc )
            {
                RespData rd = new RespData();
                rd.Reset(text);

                if ( rd.GetSuccess() )
                {
                    cb(true, "已发送");
                    return;
                }
                
            }

            cb(false, "error");
            Debug.Log(suc);
            Debug.Log(text);
        });
    }

    public static string url3 = "http://123.57.251.66/djembe/organization/getProvinceAndCityAndDistrict.action";
    public static void ReqAllSchool()
    {

        NetProxy.GetInst().ReqText(url3, null, (suc, text) => {
            if ( suc )
                DataManager.schoolData.Reset(text);
            
        });
    }


    public static string url4 = "http://123.57.251.66/djembe/main/authActivationCode.action";
    public static void ReqCDKeyValid(string key, Action<bool, string> cb )
    {
        WWWForm form = new WWWForm();
        form.AddField("code", key);
        form.AddField("unique_identifier", DeviceID.Get());
        NetProxy.GetInst().ReqText(url4, form, (suc, text)=> {
            if ( suc )
            {
                RespData rd = new RespData();
                rd.Reset(text);

                cb(rd.GetSuccess(), /*rd.Get("message")*/ text);

            }
            else
            {
                cb(false, "error");
            }
        });
    }


    public static string url5 = "http://123.57.251.66/djembe/main/sendAuthCode.action";
    public static void ReqPhoneCode(string phoneNumber, Action<bool> cb)
    {
        WWWForm form = new WWWForm();
        form.AddField("phone", phoneNumber);
        NetProxy.GetInst().ReqText(url5, form, (suc, text) => {
            if (suc)
            {
                RespData rd = new RespData();
                rd.Reset(text);

                cb(rd.GetSuccess());

            }
            else
            {
                cb(false);
            }
        });
    }


    public static string url6 = "http://123.57.251.66/djembe/organization/getOrganizationByProvinceAndCityAndDistrict.action";
    public static void ReqSchool(string province, string city, string distr, Action<bool,SchoolsData> cb)
    {
        WWWForm form = new WWWForm();
        form.AddField("province", province);
        form.AddField("city", city);
        form.AddField("district", distr);
        NetProxy.GetInst().ReqText(url6, form, (suc, text) => {
            Debug.Log(text);
            if (suc)
            {
                DataManager.schoolsData.Reset(text);

                cb(suc, DataManager.schoolsData);

            }
            else
            {
                cb(false, null);
            }
        });
    }


    public static string url7 = "http://123.57.251.66/djembe/main/sign.action";
    public static void ReqRegister(string code, string name, string account, string psd, string orgnazationid, string authcode, Action<bool,string> cb)
    {
        WWWForm form = new WWWForm();
        form.AddField("code", code);
        form.AddField("name", name);
        form.AddField("account", account);
        form.AddField("password", psd);
        form.AddField("organization_id", orgnazationid);
        form.AddField("auth_code", authcode);
        form.AddField("unique_identifier", DeviceID.Get());
        Debug.Log("unique_id :" + DeviceID.Get());
        NetProxy.GetInst().ReqText(url7, form, (suc, text) => {
            Debug.Log(text);
            if (suc)
            {
                RespData rd = new RespData();
                rd.Reset(text);
                cb(rd.GetSuccess(), rd.Get("errMsg"));

            }
            else
            {
                cb(false, "error");
            }
        });
    }


    public static string url8 = "http://123.57.251.66/djembe/main/login.action";
    public static void ReqLogin(string account, string psd, Action<bool, string> cb)
    {
        WWWForm form = new WWWForm();

        form.AddField("account", account);
        form.AddField("password", psd);
        form.AddField("unique_identifier", DeviceID.Get());

        NetProxy.GetInst().ReqText(url8, form, (suc, text) => {
            Debug.Log(text);
            if (suc)
            {
                RespData rd = new RespData();
                rd.Reset(text);
                if ( rd.GetSuccess() )
                {
                    cb(true, text);
                }
                else
                    cb(false, rd.Get("errMsg"));

            }
            else
            {
                cb(false, "error");
            }
        });
    }


    
    public static string url9 = "http://123.57.251.66/djembe/main/updateAppUser.action";
    public static void ReqForget(string account, string psd,string code, Action<bool, string> cb)
    {
        WWWForm form = new WWWForm();

        form.AddField("account", account);
        form.AddField("password", psd);
        form.AddField("auth_code", code);
        form.AddField("unique_identifier", DeviceID.Get());

        NetProxy.GetInst().ReqText(url9, form, (suc, text) => {
            Debug.Log(text);
            if (suc)
            {
                RespData rd = new RespData();
                rd.Reset(text);
                if (rd.GetSuccess())
                {
                    cb(true, "success");
                }
                else
                    cb(false, rd.Get("errMsg"));

            }
            else
            {
                cb(false, "error");
            }
        });
    }


    public static string url10 = "http://123.57.251.66/djembe/appUser/updateAppUserById.action";
    public static void ReqUpdateAppUserById(string id, string content_id = null, string name = null, Action<bool, string> cb=null)
    {
        WWWForm form = new WWWForm();


        form.AddField("id", id);

        if ( string.IsNullOrEmpty(content_id) )
        {
            form.AddField("content_id", content_id);
        }
        if (string.IsNullOrEmpty(name))
        {
            form.AddField("name", name);
        }

        NetProxy.GetInst().ReqText(url10, form, (suc, text) => {
            Debug.Log(text);
            if (suc)
            {
                RespData rd = new RespData();
                rd.Reset(text);
                if (rd.GetSuccess())
                {
                    cb(true, "success");
                }
                else
                    cb(false, "failed");

            }
            else
            {
                cb(false, "error");
            }
        });
    }


    public static string url11 = "http://123.57.251.66/djembe/organization/getOrganizationOrFacilitatorOrAdminByActivationCode.action";
    public static void ReqOrgByActCode(string actCode, Action<bool, string> cb = null)
    {
        WWWForm form = new WWWForm();


        form.AddField("code", actCode);

        NetProxy.GetInst().ReqText(url11, form, (suc, text) => {
            Debug.Log(text);

            cb(suc, text);

        });
    }



}

